import MMUI.Container
import MMUI.MMUIPackage
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.Resource
import MMUI.Widget
import MMUI.Checkbox

class Generateur {
	def void main(String[] args) {
		generateurHTML()
	}
		
	def generateurHTML() '''
		 <html>
		 	<head>
		 		"A form with HTML"
		 	</head>
		 	<body>
		 		<form>
		 			«parseContainer(getContainer("/"))»
		 		</form>
		 	</body>
		 </html>
	'''
	
	
	
	def Container getContainer(String s){
		var ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); 
		MMUIPackage.eINSTANCE.eClass()
		var resource = resourceSet.getResource(URI.createURI(s), true)
		var container = resource.contents.head as Container
		return container
	}
	
	def parseContainer(Container container){
		var widgetIterator = container.widget.iterator
		while(widgetIterator.hasNext){
			var widget = widgetIterator.next as Widget
			if(widget.class == typeof (Checkbox)) getCheckbox(widget)
			else getRadioButton(widget)
		}
	}
	
	def String getCheckbox(Widget widget){
		var result = widget.text+"\n"
		var optionIterator = widget.options.iterator
		while(optionIterator.hasNext){
			result = result+"<input type='checkbox' name='widgetCheckbox' value=''>"+optionIterator.next
		}
		return result
	}
	
	def String getRadioButton(Widget widget){
				var result = widget.text+"\n"
		var optionIterator = widget.options.iterator
		while(optionIterator.hasNext){
			result = result+"<input type='radio' name='widgetRadio' value=''>"+optionIterator.next
		}
		return result
	}

}
